package com.dyz.leetcode._10_slidingWindow;

import java.util.ArrayList;
import java.util.List;

public class _763_partitionLabels {
    public List<Integer> partitionLabels(String s) {
        // 计算并存储每个字符在数组中所在的最大索引
        int[] c2Index = new int[26];
        for (int i = 0; i < s.length(); i++) {
            c2Index[s.charAt(i) - 'a'] = i;
        }

        // 存储最终结果

        List<Integer> res = new ArrayList<>();
        //      维护窗口
        int left = 0;
        int right = 0;
        for(int i = 0; i<s.length(); i++){
            char c = s.charAt(i);
            right = Math.max(right, c2Index[c-'a']);
            if(i==right){
                res.add(right-left+1);
                left = right+1;
            }
        }
        return res;
    }
}
